package com.zlzlib.libui.weight.edittext;

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;

/**
 * @Desc: 自动调整字体大小的控件
 * @Copyright:
 * @DateTime: 2022/3/7 16:57
 * @Author zlz
 * @Version 1.0
 */
public class AutoFitTextView extends androidx.appcompat.widget.AppCompatTextView {

    private TextPaint textPaint;
    //最大的文字大小
    private float maxTextSize;
    //设置最小文字大小
    private float minTextSize;

    public AutoFitTextView(Context context) {
        this(context, null);
    }

    public AutoFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        refitText(getText().toString(), getWidth(), getHeight());
    }

    private void initialise() {
        textPaint = new TextPaint();
        textPaint.set(getPaint());
        maxTextSize = getTextSize();
        minTextSize = 10;
    }

    public void setMaxTextSize(float maxTextSize) {
        this.maxTextSize = maxTextSize;
    }

    public void setMinTextSize(float minTextSize) {
        this.minTextSize = minTextSize;
    }

    /**
     * 重新调整字体大小
     */
    private void refitText(String text, int textWidth, int textHeight) {
        if (textWidth > 0 && textHeight > 0) {
            int availableWidth = textWidth - getPaddingLeft() - getPaddingRight();
            int availableHeight = textHeight - getPaddingBottom() - getPaddingTop();
            float multiplier = getLineSpacingMultiplier();
            float add = getLineSpacingExtra();
            //将要设置成的文字大小
            float trySize = maxTextSize;
            textPaint.setTextSize(trySize);
            //当前大小的文字宽度
            float strWidth = textPaint.measureText(text);
            if (strWidth == availableWidth) {
                return;
            }
            //当文字宽度大于
            if (strWidth > availableWidth) {
                //缩小
                while (trySize > minTextSize) {
                    //获取当前文字所需的空间
                    StaticLayout layout = new StaticLayout(text, textPaint, availableWidth,
                            Layout.Alignment.ALIGN_NORMAL, multiplier, add, true);
                    int displayH = layout.getHeight();
                    //高度合适  如果设置了行数限制
                    if (getMaxLines() == -1) {
                        if (displayH < availableHeight) {
                            break;
                        }
                    } else {
                        if (layout.getLineCount() <= getMaxLines()) {
                            if (displayH < availableHeight) {
                                break;
                            }
                        }
                    }
                    trySize--;
                    if (trySize <= minTextSize) {
                        trySize += 1;
                        break;
                    }
                    textPaint.setTextSize(trySize);
                }
            }
            if (trySize != getTextSize()) {
                //设置当前文字大小
                setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
            }
        }
    }
}
